package hu.myandroidlife;

import hu.myandroidlife.base.BaseActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationActivity extends BaseActivity {
	
	private ImageView ivContent;
	private Button btnRotation;
	private Button btnTranslation;
	private Button btnAlpha;
	private Button btnScale;
	private Button btnTogether;
	

	@Override
	protected int getLayoutId() {
		return R.layout.activity_animation;
	}

	@Override
	protected void initView() {
		ivContent=$findViewById(R.id.ivContent);
		btnRotation=$findViewById(R.id.btnRotation);
		btnTranslation=$findViewById(R.id.btnTranslation);
		btnAlpha=$findViewById(R.id.btnAlpha);
		btnScale=$findViewById(R.id.btnScale);
		btnTogether=$findViewById(R.id.btnTogether);
	}

	@Override
	protected void initListener() {
		btnRotation.setOnClickListener(this);
		btnTranslation.setOnClickListener(this);
		btnAlpha.setOnClickListener(this);
		btnScale.setOnClickListener(this);
		btnTogether.setOnClickListener(this);
		
	}

	@Override
	protected void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.btnRotation:
			//通过代码实现
			// 四个参数代表，旋转的起始角度、终止角度、旋转x点、旋转y点
			RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			rotateAnimation.setDuration(2000);// 旋转时间为2秒
			ivContent.startAnimation(rotateAnimation);// 启动动画
			
			
			 //通过xml文件来实现动画效果
			// Animation rotateAnimation2 = (Animation) AnimationUtils
			// .loadAnimation(this, R.anim.myanimation);
			// ivContent.startAnimation(rotateAnimation2);
			break;
		case R.id.btnTranslation:
			//通过代码实现
			// 四个参数代表，偏移的x起始点、x终止点、偏移的y起始点、y终止点
			TranslateAnimation translateAnimation = new TranslateAnimation(0f,
					200f, 0f, 200f);
			translateAnimation.setDuration(2000);
			ivContent.startAnimation(translateAnimation);
			break;
		case R.id.btnAlpha:
			//通过代码实现
			// 两个参数代表，透明度从0升到1
			AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
			alphaAnimation.setDuration(2000);
			ivContent.startAnimation(alphaAnimation);
			break;
		case R.id.btnScale:
			//通过代码实现
			// 六个参数代表，缩放的x、y的大小，x、y的起始和终止点
			ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 1f, 0.2f, 1f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			
			AlphaAnimation alphaAnimationA = new AlphaAnimation(1f, 0f);
			AnimationSet setA = new AnimationSet(true);
			setA.addAnimation(scaleAnimation);
			setA.addAnimation(alphaAnimationA);
			setA.setDuration(2000);
			setA.setFillAfter(true);
			ivContent.startAnimation(setA);
			break;
		case R.id.btnTogether:
			RotateAnimation rotateAnimation1 = new RotateAnimation(0f, 360f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			TranslateAnimation translateAnimation1 = new TranslateAnimation(0f,
					200f, 0f, 200f);
			AlphaAnimation alphaAnimation1 = new AlphaAnimation(0f, 1f);
			ScaleAnimation scaleAnimation1 = new ScaleAnimation(0f, 1f, 0f, 1f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);

			AnimationSet set = new AnimationSet(true);
			set.addAnimation(rotateAnimation1);
			set.addAnimation(translateAnimation1);
			set.addAnimation(alphaAnimation1);
			set.addAnimation(scaleAnimation1);
			set.setDuration(2000);
			ivContent.startAnimation(set);
			break;
		}
		
	}

}
